package rickyGame.ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	
//	import ricky.core.FrameManager;
//	import ricky.core.TimeServer;
	
	/**
	 * 冷却时间倒计时图标
	 * @author pu
	 * @data 2011.6.29
	 */
	public class CoolDownIcon extends Sprite
	{
		public function CoolDownIcon()
		{
			super();
			var bd:BitmapData=new BitmapData(32,32,false,0);
			_mask=new Bitmap(bd);
			this.addChild(_mask);
			_mask.x=-16;
			_mask.y=-16;
			this.mask=_mask;
		}
		
		/**
		 * 开始倒计时
		 * @param time:Number 倒计时时间秒
		 */
		public function cooldown(time:Number):void
		{
			_beginTime=new Date().getTime()/1000;
			_perAngle=360/time;
			graphics.clear();
			//FrameManager.inst.addLogic(this.update);
		}
		
		public function update(dt:Number):void
		{
			var dtd:Number=(dt-_beginTime)/1000;
			var an:Number=dtd*_perAngle;
			DrawSector(this,an);
			if(an>=361)
			{
				this.graphics.clear();
				//FrameManager.inst.removeLogic(this.update);
				if(_endFun!=null)
					_endFun(dt);
				
			}
		}
		
		/**
		 * 画扇形
		 * @param mc:Sprite 画的容器
		 * @param angle:Number=1 扇形角度
		 * @param r:Number=100 半径
		 * @param x:Number=0 圆心x
		 * @param y:Number=0 圆心y
		 * @param startFrom:Number=270 开始角度
		 * @param color:Number 填充颜色
		 */
		private function DrawSector(mc:Sprite,angle:Number=1,r:Number=100,color:Number=0x0fcccccc,x:Number=0,y:Number=0,startFrom:Number=270):void {
			mc.graphics.beginFill(color,50);
			//mc.graphics.lineStyle(0,0x00ff0000);
			mc.graphics.moveTo(x,y);
			angle=(Math.abs(angle)>360)?360:angle;
			var n:Number=Math.ceil(Math.abs(angle)/45);
			var angleA:Number=angle/n;
			angleA=angleA*Math.PI/180;
			startFrom=startFrom*Math.PI/180;
			mc.graphics.lineTo(x+r*Math.cos(startFrom),y+r*Math.sin(startFrom));
			for (var i:int=1; i<=n; i++) {
				startFrom+=angleA;
				var angleMid:Number=startFrom-angleA/2;
				var bx:Number=x+r/Math.cos(angleA/2)*Math.cos(angleMid);
				var by:Number=y+r/Math.cos(angleA/2)*Math.sin(angleMid);
				var cx:Number=x+r*Math.cos(startFrom);
				var cy:Number=y+r*Math.sin(startFrom);
				mc.graphics.curveTo(bx,by,cx,cy);
			}
			if (angle!=360) {
				mc.graphics.lineTo(x,y);
			}
			mc.graphics.endFill();// if you want a sector without filling color , please remove this line.
		}//end function
		
	
		//private
		private var _beginTime:Number;
		private var _perAngle:Number;
		private var _mask:Bitmap;
		private var _endFun:Function;

		public function get endFun():Function
		{
			return _endFun;
		}

		public function set endFun(value:Function):void
		{
			_endFun = value;
		}

	}
}